/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.capex.javaee.urlRewrite;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Acer
 */
public class TokenServlet extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        try {
            String tokensID = request.getParameter("tokens");



            out.println("<html>");
            out.println("<head>");
            out.println("<title>Tokens</title>");
            out.println("</head>");
            out.println("<body style=\"font-family:verdana; font-size:10pt\">");

            if (tokensID == null) {
                Random rnd = new Random();
                tokensID = Long.toString(rnd.nextLong());
                out.println("<p>Welcome. A new token " + tokensID + " is now established</p>");
            } else {
                out.println("<p>Welcome back. Your Token is " + tokensID + "</p>");
            }

            String requestURLSame = request.getRequestURL().toString() + "?tokens=" + tokensID;
            String requestURLNew = request.getRequestURL().toString();
            
            out.println("<p> Click <a href=" + requestURLSame + "> here</a> again to continue browsing with the same identity. </p>");
            out.println("<p> Otherwise, click <a href=" + requestURLNew + "> here</a> to start browsing with a newidentity. </p>");


            out.println("</body>");
            out.println("</html>");
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
